using System.Linq;
using RockHopper.TrainingCalculator.WinRT.Common;
using RockHopper.TrainingCalculator.WinRT.Data;
using Windows.ApplicationModel;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

namespace RockHopper.TrainingCalculator.WinRT.Controls
{
    public class InfoControl : Control
    {
        private static readonly Style textStyle;
        private static readonly Style titleStyle;
        private static readonly IDataRepository dataRepository;

        public InfoControl()
        {
            DefaultStyleKey = typeof(InfoControl);
        }

        static InfoControl()
        {
            titleStyle = Common.Resources.TrainingStyles.Get<Style>("InfoTitleBlockStyle");
            textStyle = Common.Resources.TrainingStyles.Get<Style>("InfoTextBlockStyle");

            if (!DesignMode.DesignModeEnabled)
            {
                dataRepository = Global.Container.Resolve<IDataRepository>();    
            }            
        }

        public string InfoKey { get; set; }

        protected override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            if (DesignMode.DesignModeEnabled) return;

            var infoList = dataRepository.Get(InfoKey).ToList();
            var panel = (StackPanel)GetTemplateChild("StackPanelPart");

            foreach (var info in infoList)
            {
                if (info.StartsWith("TITLE:"))
                {
                    var title = info.Split(':')[1];
                    panel.Children.Add(new TextBlock { Style = titleStyle, Text = title });
                }
                else if (info.StartsWith("PARA:"))
                {
                    var text = info.Split(':')[1];
                    panel.Children.Add(new TextBlock { Style = textStyle, Text = text });
                }
            }
        }
    }
}